package org.syyo.admin.controller.system;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.syyo.admin.anno.CheckPermissions;
import org.syyo.admin.anno.SysLog;
import org.syyo.admin.domain.ResultVo;
import org.syyo.admin.domain.request.UserReq;
import org.syyo.admin.service.UserService;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: wangzhong
 * @Date: 2019/10/8 16:55
 * @Description: 用户模块
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @SysLog("用户添加")
    @CheckPermissions("system:user:add")
    @PostMapping("/add")
    public ResultVo addUser(@RequestBody UserReq req){
        return userService.addUser(req);
    }

    @SysLog("用户删除")
    @CheckPermissions("system:user:del")
    @PostMapping("/del")
    public ResultVo delUser(@RequestBody UserReq req){
        return  userService.delUser(req);
    }

    @SysLog("用户编辑")
    @CheckPermissions("system:user:edit")
    @PostMapping("/edit")
    public ResultVo editUser(@RequestBody UserReq req){
        return userService.editUser(req);
    }

    /**
     * 用户详情
     * @param userId
     * @return
     */
    @CheckPermissions("system:user:index")
    @GetMapping
    public ResultVo getUser(@RequestParam("userId") Integer userId){
        return userService.getUser(userId);
    }

    /**
     * 用户列表
     * @param req
     * @return
     */
    @CheckPermissions("system:user:index")
    @GetMapping("/list")
    public ResultVo listUser(UserReq req){
        return userService.listUser(req);
    }

    /**
     * 用户登录
     * @param req
     * @return
     */
    @PostMapping("/login")
    public ResultVo login(@RequestBody UserReq req){
        return userService.login(req);
    }

    /**
     * 用户退出
     * @param req
     * @return
     */
    @PostMapping("/logout")
    public ResultVo logout(@RequestBody UserReq req){

        return userService.logout(req);
    }

    /**
     * 根据用户id获取用户权限
     * @param
     * @return
     */
    @GetMapping("/menu")
    public ResultVo getShowMenuByUserId(HttpServletRequest request){
        String token = request.getHeader("X-Token");
        return userService.getShowMenuByToken(token);
    }

    /**
     * 修改密码
     * @param req
     * @return
     */
    @CheckPermissions("system:user:pwd")
    @PostMapping("/pwd")
    public ResultVo pwd(@RequestBody UserReq req){
        return userService.pwd(req);
    }

}
